package com.renwei.yunlong.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.renwei.yunlong.R;
import com.renwei.yunlong.bean.MsgBean;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.DateTimeUtils;
import com.renwei.yunlong.view.SwipeMenuLayout;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.zyf.fwms.commonlibrary.utils.GlideCircleTransform;

import butterknife.Bind;
import butterknife.ButterKnife;
import io.rong.imkit.RongIM;
import io.rong.imkit.userInfoCache.RongUserInfoManager;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Group;
import io.rong.imlib.model.UserInfo;

/**
 * Created by wangqi on 2018/1/18.
 */

public class NewsMsgAdapter extends BaseRecyclerViewAdapter<MsgBean.RowsBean> {

    private boolean isSwipe = true;

    public NewsMsgAdapter(Context context) {
        super(context);
    }

    public void setSwipe(boolean swipe) {
        isSwipe = swipe;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_message, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rholder, final int position) {
        ViewHolder holder = (ViewHolder) rholder;
        final MsgBean.RowsBean bean = getItem(position);
        holder.smlayout.setSwipeEnable(isSwipe);
        if (TextUtils.isEmpty(bean.getType())) { // 消息
            holder.updateHX(bean, position);
        } else {
            holder.updateLOCA(bean, position);
        }
        holder.listIteaseLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickLitener.onItemClick(v, position);
            }
        });
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.avatar)
        ImageView avatar;
        @Bind(R.id.avatar_container)
        RelativeLayout avatarContainer;
        @Bind(R.id.time)
        TextView time;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.msg_state)
        ImageView msgState;
        @Bind(R.id.mentioned)
        TextView mentioned;
        @Bind(R.id.message)
        TextView message;
        @Bind(R.id.unread_msg_number)
        TextView unreadMsgNumber;
        @Bind(R.id.list_itease_layout)
        RelativeLayout listIteaseLayout;
        @Bind(R.id.btn_delete)
        TextView btnDelete;
        @Bind(R.id.btn_read)
        TextView btnRead;
        @Bind(R.id.smlayout)
        SwipeMenuLayout smlayout;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void updateHX(MsgBean.RowsBean bean, final int position) {
            final Conversation conversation = bean.getConversation();
            if (conversation.getConversationType() == Conversation.ConversationType.GROUP) {
                Group group = RongUserInfoManager.getInstance().getGroupInfo(conversation.getTargetId());
                if (group != null) {
                    this.name.setText(group.getName());
                } else {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Group group = RongUserInfoManager.getInstance().getGroupInfo(conversation.getTargetId());
                            if (group != null) {
                                name.setText(group.getName());
                            }
                        }
                    }, 500);
                }
                this.avatar.setImageResource(R.mipmap.icon_group_default);
            } else if (conversation.getConversationType() == Conversation.ConversationType.PRIVATE) { //单聊
                UserInfo userInfo = RongUserInfoManager.getInstance().getUserInfo(conversation.getTargetId());
                if (userInfo != null) {
                    if ("1".equals(userInfo.getUserId())) {
                        smlayout.setSwipeEnable(false);
                    }
                    this.name.setText(userInfo.getName());
                    Glide.with(mContext).load(userInfo.getPortraitUri()).
                            error(R.mipmap.icon_header_default).transform(new GlideCircleTransform(mContext)).into(this.avatar);
                } else {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            UserInfo userInfo = RongUserInfoManager.getInstance().getUserInfo(conversation.getTargetId());
                            if (userInfo != null) {
                                if ("1".equals(userInfo.getUserId())) {
                                    smlayout.setSwipeEnable(false);
                                }
                                name.setText(userInfo.getName());
                                Glide.with(mContext).load(userInfo.getPortraitUri()).
                                        error(R.mipmap.icon_header_default).transform(new GlideCircleTransform(mContext)).into(avatar);
                            }
                        }
                    }, 500);
                }
            }
            this.mentioned.setVisibility(View.GONE);
            this.msgState.setVisibility(View.GONE);
            // 未读数
            CommonUtils.setBarTextView(this.unreadMsgNumber, conversation.getUnreadMessageCount());
            // 最近的一条消息内容
            CommonUtils.setRongIMContent(this.message, conversation.getLatestMessage());
            // 时间
            this.time.setText(DateTimeUtils.stampToDateNow(conversation.getSentTime() / 1000));

//            if (conversation.getUnreadMessageCount() > 0) {
//                btnRead.setText("已读");
//            } else {
//                btnRead.setText("未读");
            btnRead.setVisibility(View.GONE);
//            }

            this.btnDelete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // 删除会话
                    smlayout.smoothClose();
                    RongIM.getInstance().removeConversation(conversation.getConversationType(), conversation.getTargetId());
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            removeData(position);
                        }
                    }, 500);
                }
            });
            this.btnRead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // 已读
                    smlayout.smoothClose();
                    RongIM.getInstance().clearMessagesUnreadStatus(conversation.getConversationType(), conversation.getTargetId());
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            btnRead.setVisibility(View.GONE);
                            unreadMsgNumber.setVisibility(View.INVISIBLE);
                        }
                    }, 500);
                }
            });

        }

        public void updateLOCA(final MsgBean.RowsBean bean, final int position) {

            String name = "";
            Integer idRes = -1;
            switch (bean.getType().substring(0, 1)) {
                //0工单1问题 2投诉 3对象 4备件 5项目 6系统
                case "0":
                    name = "工单";
                    idRes = R.mipmap.icon_gongdan;
                    break;
                case "1":
                    if ("16".equals(bean.getType())) {
                        name = "知识";
                        idRes = R.mipmap.icon_zhishi;
                    } else {
                        name = "问题";
                        idRes = R.mipmap.icon_wenti;
                    }
                    break;
                case "2":
                    name = "投诉";
                    idRes = R.mipmap.icon_tousu;
                    break;
                case "3":
                    name = "对象";
                    idRes = R.mipmap.icon_duixiang;
                    break;
                case "4":
                    name = "备件";
                    idRes = R.mipmap.icon_beijian;
                    break;
                case "5":
                    name = "项目";
                    idRes = R.mipmap.icon_xiangmu;
                    break;
                case "6":
                    name = "企业";
                    idRes = R.mipmap.icon_qiye;
                    break;
                case "7":

                    break;
                default:
                    break;
            }
            Glide.with(mContext).load(idRes).
                    transform(new GlideCircleTransform(mContext)).into(this.avatar);
            this.name.setText(name);
            this.message.setText(bean.getContent());
            if (!TextUtils.isEmpty(bean.getCreateDate())) {
                String data = bean.getCreateDate().replace("T", " ");
                this.time.setText(String.valueOf(DateTimeUtils.stampToDateNow(DateTimeUtils.string2Long(data, "yyyy-MM-dd HH:mm:ss"))));
            }
            btnRead.setVisibility(View.VISIBLE);
            if (bean.getQuantity() != 0) {
                this.unreadMsgNumber.setVisibility(View.VISIBLE);
                this.unreadMsgNumber.setText(bean.getQuantity() + "");
                btnRead.setText("已读");
            } else {
                btnRead.setText("未读");
                this.unreadMsgNumber.setVisibility(View.GONE);
            }

            this.btnDelete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // 删除会话
                    mOnItemClickLitener.onItemClick(view, position);
                    smlayout.smoothClose();
                }
            });
            this.btnRead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // 已读
                    mOnItemClickLitener.onItemClick(view, position);
                    smlayout.smoothClose();
                }
            });
        }
    }
}

